package com.yybray.reduce;

import org.junit.Test;

import java.util.Arrays;
import java.util.List;
import java.util.Optional;

/**
 * Created by Bray.ye on 2017/03/24 10:06.
 */
public class Reduce {

    List<Integer> nums = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9);
    List<Integer> numsNull = Arrays.asList();


    @Test
    public void name1() throws Exception {
        //归约
        //求和
        nums.stream().reduce(0, (x, y) -> x + y);
        nums.stream().reduce(0, Integer::sum);
        //求最大值最小值
        //min
        Optional<Integer> min1 = nums.stream().reduce((x, y) -> x < y ? x : y);
        Optional<Integer> min2 = numsNull.stream().reduce(Integer::min);
        Integer integer = min2.orElse(0);
        //max
        Optional<Integer> max1 = nums.stream().reduce((x, y) -> x > y ? x : y);
        Optional<Integer> max2 = nums.stream().reduce(Integer::max);
    }
}
